using System;
using Nuwanda.TfsSpotlight.Shell.Exceptions;
using Nuwanda.TfsSpotlight.Shell.Services;

namespace Nuwanda.TfsSpotlight.Shell.Expressions
{
    internal class ActivatorConfigurationExpression : IActivatorConfigurationExpression
    {
        private readonly CommandConfiguration _configuration;
        private Func<bool> _activator;
        private object _context;

        public ActivatorConfigurationExpression(CommandConfiguration configuration)
        {
            this._configuration = configuration;
        }

        public void Commit()
        {
            if(this._context == null)
                throw new MenuConfigurationException("You have to specify a context for an activator.");

            if(this._activator == null)
                throw  new MenuConfigurationException("Tou have to specify an activation delegate.");

            if(this._configuration.Activators.ContainsKey(this._context) == false)
            {
                this._configuration.Activators.Add(this._context, this._activator);
            }
            else
            {
                this._configuration.Activators[this._context] = this._activator;
            }
        }

        public IActivatorConfigurationExpression Activator(Func<bool> activator)
        {
            this._activator = activator;
            return this;
        }

        public IActivatorConfigurationExpression Context(object context)
        {
            this._context = context;
            return this;
        }
    }
}